package com.twotiger.and.view;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import app.twotiger.p2p.R;

public class AcountTitlePop extends PopupWindow {

    private View conentView;
    private TextView itemtext;
    private final static int POP_DAYS_INVERST = 10;
    private final static int POP_TOTAL_ASSET = 11;
    private final static int POP_ASSOON_CAPITAL = 12;
    private final static int POP_INCOME_CAPITAL = 13;
    private final static int POP_TOTAL_INVERST = 14;

    public AcountTitlePop(Context context, final TextView titleview, final Handler handler) {
        super(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        WindowManager windowmanager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
        conentView = inflater.inflate(R.layout.title_popup_layout, null);
        // 设置SelectPicPopupWindow的View
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        // 刷新状态
        // 实例化一个ColorDrawable颜色为半透明
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        setContentView(conentView);
        TextView days_inverst = (TextView) conentView.findViewById(R.id.days_inverst);
        TextView total_asset = (TextView) conentView.findViewById(R.id.total_asset);
        TextView assoon_capital = (TextView) conentView.findViewById(R.id.assoon_capital);
        TextView income_capital = (TextView) conentView.findViewById(R.id.income_capital);
        TextView total_inverst = (TextView) conentView.findViewById(R.id.total_inverst);
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                itemtext = (TextView) v;
                String itemstr = itemtext.getText().toString();
                String titlestr = titleview.getText().toString();
                if (titlestr.equals(itemstr)) {
                    return;
                }
                titleview.setText(itemtext.getText().toString());
                switch (v.getId()) {
                case R.id.days_inverst:
                    handler.sendEmptyMessage(POP_DAYS_INVERST);
                    break;
                case R.id.total_asset:
                    handler.sendEmptyMessage(POP_TOTAL_ASSET);
                    break;
                case R.id.assoon_capital:
                    handler.sendEmptyMessage(POP_ASSOON_CAPITAL);
                    break;
                case R.id.income_capital:
                    handler.sendEmptyMessage(POP_INCOME_CAPITAL);
                    break;
                case R.id.total_inverst:
                    handler.sendEmptyMessage(POP_TOTAL_INVERST);
                    break;
                default:
                    break;
                }

            }
        };
        days_inverst.setOnClickListener(clickListener);
        total_asset.setOnClickListener(clickListener);
        assoon_capital.setOnClickListener(clickListener);
        income_capital.setOnClickListener(clickListener);
        total_inverst.setOnClickListener(clickListener);
    }

    /**
     * 显示popupWindow
     * 
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }

}
